var hotapp = require('../../utils/hotapp.js')
Page({
  data: {
    searchData: '',
    musicList: {},
    searchList: {},
    hotHidden: 'block',
    searchHidden: 'none'
  },

  // 获取热门歌曲
  onReady: function () {
    const that = this
    let hot_url = 'http://m.kugou.com/?json=true'
    hotapp.request({
      useProxy: true,
      url: hot_url,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        let result = res.data.data
        that.setData({
          musicList: result
        })
      }
    })
  },

  // 搜索歌曲
  searchSong: function () {
    const that = this
    let keyword = that.data.searchData
    if (keyword) {
      let search_url = `http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword=${keyword}&page=1&pagesize=20&showtype=1`
      hotapp.request({
        useProxy: true,
        url: search_url,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.status) {
            let result = res.data.data.info
            that.setData({
              searchList: result,
              hotHidden: 'none',
              searchHidden: 'block'
            })
          } else {
            console.log('搜索失败')
          }
        }
      })
    }
  },

  // 获取搜索框内容
  getSearch: function (e) {
    const that = this
    let inputVal = e.detail.value
    that.setData({
      searchData: inputVal
    })
  },

  // 播放选择的音乐
  playMusic: function (e) {
    let musicHash = e.target.dataset.hash
    wx.navigateTo({
      url: `../play/play?hash=${musicHash}`,
    })
  }
})
